package com.fwzc.giuseppe.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.base.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class UpdateInfoActivity extends BaseActivity implements OnClickListener {
	private EditText content;
	private TextView type;
	private int flage;
	private ProgressDialog load;
	private HttpUtils httpUtil;
	private String userId, userIdentityNumber;
	private String weight = "", height = "", name = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_user_info);

		initBar();
		initView();
		initData();
	}

	private void initView() {
		// 网络请求相关
		httpUtil = new HttpUtils();
		load = new ProgressDialog(this);
		load.setMessage(getResources().getString(R.string.deal));
		load.setCancelable(false);

		actionbar_side_name.setText("信息更新");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		tv_actionbar_side_right.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);
		tv_actionbar_side_right.setText("保存");
		tv_actionbar_side_right.setOnClickListener(this);

		content = (EditText) findViewById(R.id.content);
		type = (TextView) findViewById(R.id.type);
	}

	private void initData() {
		flage = getIntent().getIntExtra("flage", -1);
		userId = getIntent().getStringExtra("userId");
		userIdentityNumber = getIntent().getStringExtra("userIdentityNumber");
		switch (flage) {
		case 1:
			type.setText("请认真填写身高信息,非常重要。单位cm");
			break;

		case 2:
			type.setText("请认真填写体重信息,非常重要。单位kg");
			break;

		case 3:
			type.setText("妈妈说填写了名字更洋气哦");
			content.setInputType(InputType.TYPE_CLASS_TEXT);
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_actionbar_side_right:
			if ("".equals(content.getText().toString())) {
				showToast("请填写相应信息");
				return;
			}

			switch (flage) {
			case 1:
				height = content.getText().toString();
				break;

			case 2:
				weight = content.getText().toString();
				break;

			case 3:
				name = content.getText().toString();
				break;
			default:
				break;
			}
			updateInfo();
			break;
		case R.id.iv_actionbar_side_left:
			finish();
			break;

		default:
			break;
		}
	}

	// 提交更改
	private void updateInfo() {
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("type", "1");
		params.addBodyParameter("userId", userId);
		params.addBodyParameter("userIdentityNumber", userIdentityNumber);
		params.addBodyParameter("imageURL", "");
		params.addBodyParameter("gender", "");
		params.addBodyParameter("weight", weight);
		params.addBodyParameter("height", height);
		params.addBodyParameter("birthday", "");
		params.addBodyParameter("schoolId", "");
		params.addBodyParameter("classId", "");
		params.addBodyParameter("name", name);
		params.addBodyParameter("gardeId", "");
		// 发送json类型的数据
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.GET_USERINFO, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						showToast("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(
									responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 5) {
								load.dismiss();
								Intent intent = new Intent();
								intent.putExtra("value", content.getText().toString());
								setResult(RESULT_OK, intent);
								finish();
							} else {
								load.dismiss();
								showToast(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							showToast("解析出错");
						}

					}
				});
	}
}
